//
//  NWHomeCellTop.swift
//  微博01
//
//  Created by 牛威 on 15/12/11.
//  Copyright © 2015年 牛威. All rights reserved.
//

import UIKit


class NWHomeCellTop: UIView {
    
    
    var arrayM : HMViewModel? {
        
        didSet{
            
            //设置数据内容
         
            nameLabel.text = arrayM?.homeModel.user?.screen_name
            
            iconView.sd_setImageWithURL(arrayM?.userIconUrl, placeholderImage: arrayM?.userDefaultImage)
            
            memberView.image = arrayM?.userMemberImage
            
            vipView.image  = arrayM?.userVipImage
            
            
            //TODO: - 后续改进
            timeLabel.text =  "现在"               //arrayM?.homeModel.created_at
            sourceLabel.text = "牛逼的iPhone"      //arrayM?.homeModel.source
            
        }
        
        
        
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //懒加载控件
    
    /// 头像视图
    private lazy var iconView:UIImageView = UIImageView(imageName: "avatar_default_big")
    //认证达人
    private lazy var vipView:UIImageView = UIImageView(imageName: "avatar_grassroot")
    //昵称
    private lazy var nameLabel : UILabel = UILabel(title: "就不穿小内", fontSize: 14)
    //会员等级视图
    private lazy var memberView : UIImageView = UIImageView(imageName: "common_icon_membership_level6")
    //创建时间
    private lazy var timeLabel : UILabel = UILabel(title: "刚刚", fontSize: 11, color:.orangeColor())
    //来自哪里发的
    private lazy var sourceLabel : UILabel = UILabel(title: "牛逼的iPhone", fontSize: 11)

   private func setupUI(){
        
//        backgroundColor = UIColor(white: 0.9, alpha: 1)

        //0添加分割视图
        
        let sepView = UIView()
        sepView.backgroundColor = UIColor.lightGrayColor()
        addSubview(sepView)
        
          //设置分割布局 在这里这个视图只需要用一次 便不会再次访问 而至于懒加载是全局的 还需要设置问题 图片等问题
        // 在这里也不需要设置它的属性之类的东西 所以用实例化
        
          sepView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.height.equalTo(TopViewMargin) //这里的高度加了一个距离你的cell的高度也需要修改要不然会挤
        }
        
        
        
        
        //1添加控件

        
        addSubview(iconView)
        addSubview(vipView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(memberView)
        addSubview(sourceLabel)
        
        
        //2自动布局
        
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left).offset(TopViewMargin)
            make.top.equalTo(sepView.snp_bottom).offset(TopViewMargin)
            make.width.equalTo(TopIconViewWidth)
            make.height.equalTo(TopIconViewWidth)
        }
        
        vipView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(iconView.snp_right).offset(TopViewMargin)
            make.top.equalTo(iconView.snp_top)
        }
        
        memberView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(nameLabel.snp_right).offset(TopViewMargin)
            make.top.equalTo(nameLabel.snp_top)

        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(iconView.snp_right).offset(TopViewMargin)
            make.bottom.equalTo(iconView.snp_bottom)
            
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(timeLabel.snp_right).offset(TopViewMargin)
            make.bottom.equalTo(iconView.snp_bottom)
            

        }
    
    }
    
    
}
